home *** CD-ROM | disk | FTP | other *** search
/ Aminet 32 / Aminet 32 (1999)(Schatztruhe)[!][Aug 1999].iso / Aminet / dev / lang / Python151_Src.lha / Python1.5_Source / Python / sysmodule.c < prev    next >
C/C++ Source or Header  |  1998-06-01  |  12KB  |  499 lines

  1. /***********************************************************
  2. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
  3. The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its
  8. documentation for any purpose and without fee is hereby granted,
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI or Corporation for National Research Initiatives or
  13. CNRI not be used in advertising or publicity pertaining to
  14. distribution of the software without specific, written prior
  15. permission.
  16.  
  17. While CWI is the initial source for this software, a modified version
  18. is made available by the Corporation for National Research Initiatives
  19. (CNRI) at the Internet address ftp://ftp.python.org.
  20.  
  21. STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
  22. REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  23. MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
  24. CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  25. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  26. PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  27. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  28. PERFORMANCE OF THIS SOFTWARE.
  29.  
  30. ******************************************************************/
  31.  
  32. /* System module */
  33.  
  34. /*
  35. Various bits of information used by the interpreter are collected in
  36. module 'sys'.
  37. Function member:
  38. - exit(sts): raise SystemExit
  39. Data members:
  40. - stdin, stdout, stderr: standard file objects
  41. - modules: the table of modules (dictionary)
  42. - path: module search path (list of strings)
  43. - argv: script arguments (list of strings)
  44. - ps1, ps2: optional primary and secondary prompts (strings)
  45. */
  46.  
  47. #include "Python.h"
  48.  
  49. #include "osdefs.h"
  50.  
  51. #include "protos/sysmodule_protos.h"
  52.  
  53. #ifdef HAVE_UNISTD_H
  54. #include <unistd.h>
  55. #endif
  56.  
  57. #ifdef MS_COREDLL
  58. extern void *PyWin_DLLhModule;
  59. /* A string loaded from the DLL at startup: */
  60. extern const char *PyWin_DLLVersionString;
  61. #endif
  62.  
  63. PyObject *
  64. PySys_GetObject(name)
  65.     char *name;
  66. {
  67.     PyThreadState *tstate = PyThreadState_Get();
  68.     PyObject *sd = tstate->interp->sysdict;
  69.     return PyDict_GetItemString(sd, name);
  70. }
  71.  
  72. FILE *
  73. PySys_GetFile(name, def)
  74.     char *name;
  75.     FILE *def;
  76. {
  77.     FILE *fp = NULL;
  78.     PyObject *v = PySys_GetObject(name);
  79.     if (v != NULL && PyFile_Check(v))
  80.         fp = PyFile_AsFile(v);
  81.     if (fp == NULL)
  82.         fp = def;
  83.     return fp;
  84. }
  85.  
  86. int
  87. PySys_SetObject(name, v)
  88.     char *name;
  89.     PyObject *v;
  90. {
  91.     PyThreadState *tstate = PyThreadState_Get();
  92.     PyObject *sd = tstate->interp->sysdict;
  93.     if (v == NULL) {
  94.         if (PyDict_GetItemString(sd, name) == NULL)
  95.             return 0;
  96.         else
  97.             return PyDict_DelItemString(sd, name);
  98.     }
  99.     else
  100.         return PyDict_SetItemString(sd, name, v);
  101. }
  102.  
  103. static PyObject *
  104. sys_exc_info(self, args)
  105.     PyObject *self;
  106.     PyObject *args;
  107. {
  108.     PyThreadState *tstate;
  109.     if (!PyArg_Parse(args, ""))
  110.         return NULL;
  111.     tstate = PyThreadState_Get();
  112.     return Py_BuildValue(
  113.         "(OOO)",
  114.         tstate->exc_type != NULL ? tstate->exc_type : Py_None,
  115.         tstate->exc_value != NULL ? tstate->exc_value : Py_None,
  116.         tstate->exc_traceback != NULL ?
  117.             tstate->exc_traceback : Py_None);
  118. }
  119.  
  120. static PyObject *
  121. sys_exit(self, args)
  122.     PyObject *self;
  123.     PyObject *args;
  124. {
  125.     /* Raise SystemExit so callers may catch it or clean up. */
  126.     PyErr_SetObject(PyExc_SystemExit, args);
  127.     return NULL;
  128. }
  129.  
  130. static PyObject *
  131. sys_settrace(self, args)
  132.     PyObject *self;
  133.     PyObject *args;
  134. {
  135.     PyThreadState *tstate = PyThreadState_Get();
  136.     if (args == Py_None)
  137.         args = NULL;
  138.     else
  139.         Py_XINCREF(args);
  140.     Py_XDECREF(tstate->sys_tracefunc);
  141.     tstate->sys_tracefunc = args;
  142.     Py_INCREF(Py_None);
  143.     return Py_None;
  144. }
  145.  
  146. static PyObject *
  147. sys_setprofile(self, args)
  148.     PyObject *self;
  149.     PyObject *args;
  150. {
  151.     PyThreadState *tstate = PyThreadState_Get();
  152.     if (args == Py_None)
  153.         args = NULL;
  154.     else
  155.         Py_XINCREF(args);
  156.     Py_XDECREF(tstate->sys_profilefunc);
  157.     tstate->sys_profilefunc = args;
  158.     Py_INCREF(Py_None);
  159.     return Py_None;
  160. }
  161.  
  162. static PyObject *
  163. sys_setcheckinterval(self, args)
  164.     PyObject *self;
  165.     PyObject *args;
  166. {
  167.     PyThreadState *tstate = PyThreadState_Get();
  168.     if (!PyArg_ParseTuple(args, "i", &tstate->interp->checkinterval))
  169.         return NULL;
  170.     Py_INCREF(Py_None);
  171.     return Py_None;
  172. }
  173.  
  174. #ifdef USE_MALLOPT
  175. /* Link with -lmalloc (or -lmpc) on an SGI */
  176. #include <malloc.h>
  177.  
  178. static PyObject *
  179. sys_mdebug(self, args)
  180.     PyObject *self;
  181.     PyObject *args;
  182. {
  183.     int flag;
  184.     if (!PyArg_Parse(args, "i", &flag))
  185.         return NULL;
  186.     mallopt(M_DEBUG, flag);
  187.     Py_INCREF(Py_None);
  188.     return Py_None;
  189. }
  190. #endif /* USE_MALLOPT */
  191.  
  192. static PyObject *
  193. sys_getrefcount(self, args)
  194.     PyObject *self;
  195.     PyObject *args;
  196. {
  197.     PyObject *arg;
  198.     if (!PyArg_Parse(args, "O", &arg))
  199.         return NULL;
  200.     return PyInt_FromLong((long) arg->ob_refcnt);
  201. }
  202.  
  203. #ifdef COUNT_ALLOCS
  204. static PyObject *
  205. sys_getcounts(self, args)
  206.     PyObject *self, *args;
  207. {
  208.     extern PyObject *get_counts Py_PROTO((void));
  209.  
  210.     if (!PyArg_Parse(args, ""))
  211.         return NULL;
  212.     return get_counts();
  213. }
  214. #endif
  215.  
  216. #ifdef Py_TRACE_REFS
  217. /* Defined in objects.c because it uses static globals if that file */
  218. extern PyObject *_Py_GetObjects Py_PROTO((PyObject *, PyObject *));
  219. #endif
  220.  
  221. #ifdef DYNAMIC_EXECUTION_PROFILE
  222. /* Defined in ceval.c because it uses static globals if that file */
  223. extern PyObject *_Py_GetDXProfile Py_PROTO((PyObject *,  PyObject *));
  224. #endif
  225.  
  226. static PyMethodDef sys_methods[] = {
  227.     /* Might as well keep this in alphabetic order */
  228.     {"exc_info",    sys_exc_info, 0},
  229.     {"exit",    sys_exit, 0},
  230. #ifdef COUNT_ALLOCS
  231.     {"getcounts",    sys_getcounts, 0},
  232. #endif
  233. #ifdef DYNAMIC_EXECUTION_PROFILE
  234.     {"getdxp",    _Py_GetDXProfile, 1},
  235. #endif
  236. #ifdef Py_TRACE_REFS
  237.     {"getobjects",    _Py_GetObjects, 1},
  238. #endif
  239.     {"getrefcount",    sys_getrefcount, 0},
  240. #ifdef USE_MALLOPT
  241.     {"mdebug",    sys_mdebug, 0},
  242. #endif
  243.     {"setcheckinterval",    sys_setcheckinterval, 1},
  244.     {"setprofile",    sys_setprofile, 0},
  245.     {"settrace",    sys_settrace, 0},
  246.     {NULL,        NULL}        /* sentinel */
  247. };
  248.  
  249. static PyObject *
  250. list_builtin_module_names()
  251. {
  252.     PyObject *list = PyList_New(0);
  253.     int i;
  254.     if (list == NULL)
  255.         return NULL;
  256.     for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
  257.         PyObject *name = PyString_FromString(
  258.             PyImport_Inittab[i].name);
  259.         if (name == NULL)
  260.             break;
  261.         PyList_Append(list, name);
  262.         Py_DECREF(name);
  263.     }
  264.     if (PyList_Sort(list) != 0) {
  265.         Py_DECREF(list);
  266.         list = NULL;
  267.     }
  268.     if (list) {
  269.         PyObject *v = PyList_AsTuple(list);
  270.         Py_DECREF(list);
  271.         list = v;
  272.     }
  273.     return list;
  274. }
  275.  
  276. PyObject *
  277. _PySys_Init()
  278. {
  279.     extern int fclose Py_PROTO((FILE *));
  280.     PyObject *m, *v, *sysdict;
  281.     PyObject *sysin, *sysout, *syserr;
  282.  
  283.     m = Py_InitModule("sys", sys_methods);
  284.     sysdict = PyModule_GetDict(m);
  285.  
  286.     sysin = PyFile_FromFile(stdin, "<stdin>", "r", NULL);
  287.     sysout = PyFile_FromFile(stdout, "<stdout>", "w", NULL);
  288.     syserr = PyFile_FromFile(stderr, "<stderr>", "w", NULL);
  289.     if (PyErr_Occurred())
  290.         return NULL;
  291.     PyDict_SetItemString(sysdict, "stdin", sysin);
  292.     PyDict_SetItemString(sysdict, "stdout", sysout);
  293.     PyDict_SetItemString(sysdict, "stderr", syserr);
  294.     /* Make backup copies for cleanup */
  295.     PyDict_SetItemString(sysdict, "__stdin__", sysin);
  296.     PyDict_SetItemString(sysdict, "__stdout__", sysout);
  297.     PyDict_SetItemString(sysdict, "__stderr__", syserr);
  298.     Py_XDECREF(sysin);
  299.     Py_XDECREF(sysout);
  300.     Py_XDECREF(syserr);
  301.     PyDict_SetItemString(sysdict, "version",
  302.                  v = PyString_FromString(Py_GetVersion()));
  303.     Py_XDECREF(v);
  304.     PyDict_SetItemString(sysdict, "copyright",
  305.                  v = PyString_FromString(Py_GetCopyright()));
  306.     Py_XDECREF(v);
  307.     PyDict_SetItemString(sysdict, "platform",
  308.                  v = PyString_FromString(Py_GetPlatform()));
  309.     Py_XDECREF(v);
  310.     PyDict_SetItemString(sysdict, "executable",
  311.                  v = PyString_FromString(Py_GetProgramFullPath()));
  312.     Py_XDECREF(v);
  313.     PyDict_SetItemString(sysdict, "prefix",
  314.                  v = PyString_FromString(Py_GetPrefix()));
  315.     Py_XDECREF(v);
  316.     PyDict_SetItemString(sysdict, "exec_prefix",
  317.            v = PyString_FromString(Py_GetExecPrefix()));
  318.     Py_XDECREF(v);
  319.     PyDict_SetItemString(sysdict, "maxint",
  320.                  v = PyInt_FromLong(PyInt_GetMax()));
  321.     Py_XDECREF(v);
  322.     PyDict_SetItemString(sysdict, "builtin_module_names",
  323.            v = list_builtin_module_names());
  324.     Py_XDECREF(v);
  325. #ifdef MS_COREDLL
  326.     PyDict_SetItemString(sysdict, "dllhandle",
  327.                  v = PyInt_FromLong((int)PyWin_DLLhModule));
  328.     Py_XDECREF(v);
  329.     PyDict_SetItemString(sysdict, "winver",
  330.                  v = PyString_FromString(PyWin_DLLVersionString));
  331.     Py_XDECREF(v);
  332. #endif
  333.     if (PyErr_Occurred())
  334.         return NULL;
  335.     return m;
  336. }
  337.  
  338. static PyObject *
  339. makepathobject(path, delim)
  340.     char *path;
  341.     int delim;
  342. {
  343.     int i, n;
  344.     char *p;
  345.     PyObject *v, *w;
  346.     
  347.     n = 1;
  348.     p = path;
  349.     while ((p = strchr(p, delim)) != NULL) {
  350.         n++;
  351.         p++;
  352.     }
  353.     v = PyList_New(n);
  354.     if (v == NULL)
  355.         return NULL;
  356.     for (i = 0; ; i++) {
  357.         p = strchr(path, delim);
  358.         if (p == NULL)
  359.             p = strchr(path, '\0'); /* End of string */
  360.         w = PyString_FromStringAndSize(path, (int) (p - path));
  361.         if (w == NULL) {
  362.             Py_DECREF(v);
  363.             return NULL;
  364.         }
  365.         PyList_SetItem(v, i, w);
  366.         if (*p == '\0')
  367.             break;
  368.         path = p+1;
  369.     }
  370.     return v;
  371. }
  372.  
  373. void
  374. PySys_SetPath(path)
  375.     char *path;
  376. {
  377.     PyObject *v;
  378.     if ((v = makepathobject(path, DELIM)) == NULL)
  379.         Py_FatalError("can't create sys.path");
  380.     if (PySys_SetObject("path", v) != 0)
  381.         Py_FatalError("can't assign sys.path");
  382.     Py_DECREF(v);
  383. }
  384.  
  385. static PyObject *
  386. makeargvobject(argc, argv)
  387.     int argc;
  388.     char **argv;
  389. {
  390.     PyObject *av;
  391.     if (argc <= 0 || argv == NULL) {
  392.         /* Ensure at least one (empty) argument is seen */
  393.         static char *empty_argv[1] = {""};
  394.         argv = empty_argv;
  395.         argc = 1;
  396.     }
  397.     av = PyList_New(argc);
  398.     if (av != NULL) {
  399.         int i;
  400.         for (i = 0; i < argc; i++) {
  401.             PyObject *v = PyString_FromString(argv[i]);
  402.             if (v == NULL) {
  403.                 Py_DECREF(av);
  404.                 av = NULL;
  405.                 break;
  406.             }
  407.             PyList_SetItem(av, i, v);
  408.         }
  409.     }
  410.     return av;
  411. }
  412.  
  413. void
  414. PySys_SetArgv(argc, argv)
  415.     int argc;
  416.     char **argv;
  417. {
  418.     PyObject *av = makeargvobject(argc, argv);
  419.     PyObject *path = PySys_GetObject("path");
  420.     if (av == NULL)
  421.         Py_FatalError("no mem for sys.argv");
  422.     if (PySys_SetObject("argv", av) != 0)
  423.         Py_FatalError("can't assign sys.argv");
  424.     if (path != NULL) {
  425.         char *argv0 = argv[0];
  426.         char *p = NULL;
  427.         int n = 0;
  428.         PyObject *a;
  429. #ifdef HAVE_READLINK
  430.         char link[MAXPATHLEN+1];
  431.         char argv0copy[2*MAXPATHLEN+1];
  432.         int nr = 0;
  433.         if (argc > 0 && argv0 != NULL)
  434.             nr = readlink(argv0, link, MAXPATHLEN);
  435.         if (nr > 0) {
  436.             /* It's a symlink */
  437.             link[nr] = '\0';
  438.             if (link[0] == SEP)
  439.                 argv0 = link; /* Link to absolute path */
  440.             else if (strchr(link, SEP) == NULL)
  441.                 ; /* Link without path */
  442.             else {
  443.                 /* Must join(dirname(argv0), link) */
  444.                 char *q = strrchr(argv0, SEP);
  445.                 if (q == NULL)
  446.                     argv0 = link; /* argv0 without path */
  447.                 else {
  448.                     /* Must make a copy */
  449.                     strcpy(argv0copy, argv0);
  450.                     q = strrchr(argv0copy, SEP);
  451.                     strcpy(q+1, link);
  452.                     argv0 = argv0copy;
  453.                 }
  454.             }
  455.         }
  456. #endif /* HAVE_READLINK */
  457. #if SEP == '\\' /* Special case for MS filename syntax */
  458.         if (argc > 0 && argv0 != NULL) {
  459.             char *q;
  460.             p = strrchr(argv0, SEP);
  461.             /* Test for alternate separator */
  462.             q = strrchr(p ? p : argv0, '/');
  463.             if (q != NULL)
  464.                 p = q;
  465.             if (p != NULL) {
  466.                 n = p + 1 - argv0;
  467.                 if (n > 1 && p[-1] != ':')
  468.                     n--; /* Drop trailing separator */
  469.             }
  470.         }
  471. #else /* All other filename syntaxes */
  472.         if (argc > 0 && argv0 != NULL)
  473.             p = strrchr(argv0, SEP);
  474.         if (p != NULL) {
  475.             n = p + 1 - argv0;
  476. #if SEP == '/' /* Special case for Unix filename syntax */
  477.             if (n > 1)
  478.                 n--; /* Drop trailing separator */
  479. #endif /* Unix */
  480.         }
  481. #ifdef _AMIGA
  482.         else
  483.         {
  484.             /* check for absolute paths on Amiga */
  485.             if(argc>0 && argv0!=NULL) p=strrchr(argv0,':');
  486.             if(p!=NULL)     n=p+1-argv0;
  487.         }
  488. #endif /* _AMIGA */
  489. #endif /* All others */
  490.         a = PyString_FromStringAndSize(argv0, n);
  491.         if (a == NULL)
  492.             Py_FatalError("no mem for sys.path insertion");
  493.         if (PyList_Insert(path, 0, a) < 0)
  494.             Py_FatalError("sys.path.insert(0) failed");
  495.         Py_DECREF(a);
  496.     }
  497.     Py_DECREF(av);
  498. }
  499.